package sk.octopuss.archery.service.impl;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import sk.octopuss.archery.service.ImageService;

@Component
public class ImageServiceImpl implements ImageService {

	@Value("${thumbnail.width}")
	private int tbWidth;
	@Value("${thumbnail.height}")
	private int tbHeight;
	
	@Value("${thumbnail.ratio}")
	private double tbRatio;
	
	
	@Value("${image.tempFolder}")
	private String tempFolder;
	
	

	@Override
	public BufferedImage downloadImage(final URL url) {
		Image image = null;
		try {
			image = ImageIO.read(url);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final BufferedImage cpimg = bufferImage(image);
		return cpimg;
	}

	private BufferedImage bufferImage(final Image image) {
		return bufferImage(image, BufferedImage.TYPE_INT_RGB);
	}

	private BufferedImage bufferImage(final Image image, final int type) {
		final BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
		final Graphics2D g = bufferedImage.createGraphics();
		g.drawImage(image, null, null);
		return bufferedImage;
	}

	@Override
	public BufferedImage generateThumbNail(final File file) {

		final BufferedImage img = new BufferedImage(tbWidth, tbHeight, BufferedImage.TYPE_INT_RGB);
		try {
			img.createGraphics().drawImage(ImageIO.read(file).getScaledInstance(tbWidth, tbHeight, Image.SCALE_SMOOTH),
					0, 0, null);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return img;
	}

	@Override
	public BufferedImage generateThumbNail(final URL url) {
		final BufferedImage img = new BufferedImage(tbWidth, tbHeight, BufferedImage.TYPE_INT_RGB);
		try {
			img.createGraphics().drawImage(ImageIO.read(url).getScaledInstance(tbWidth, tbHeight, Image.SCALE_SMOOTH),
					0, 0, null);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return img;
	}

	@Override
	public boolean saveImage(final Image img, final File file) {
		
		try {
			ImageIO.write(bufferImage(img), "jpg", file);
			return true;
		} catch (final IOException e) {
			e.printStackTrace();
			return false;
		
		}
		
	}

	@Override
	public File downloadImageFile(final URL url) {
		Image image = null;
		File out = null;
		try {
			image = ImageIO.read(url);
			out = new File(tempFolder + "test.jpg");
			ImageIO.write(bufferImage(image), "jpg", out);
		} catch (final IOException e) {
		}
		return out;

	}
	
	

	@Override
	public BufferedImage scaleImage(final File file) {
		return null;
		
	}

	@Override
	public BufferedImage scaleImage(final URL url) {
		BufferedImage original = null;
		int tbHeight,tbWidth;
		BufferedImage img = null;
		
		try {
			original = ImageIO.read(url);
		
		tbHeight = (int) (original.getHeight()*tbRatio);
		tbWidth = (int)(original.getWidth()*tbRatio);
		
		img = new BufferedImage(tbWidth, tbHeight, BufferedImage.TYPE_INT_RGB);
		img.createGraphics().drawImage(original.getScaledInstance(tbWidth, tbHeight, Image.SCALE_SMOOTH),
					0, 0, null);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return img;
		
		
	}

	@Override
	public BufferedImage generateThumbNail(final String uri) {
		URL url = null;
		try {
			url = new URL(uri);
		} catch (final MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		final BufferedImage img = new BufferedImage(tbWidth, tbHeight, BufferedImage.TYPE_INT_RGB);
		try {
			img.createGraphics().drawImage(ImageIO.read(url).getScaledInstance(tbWidth, tbHeight, Image.SCALE_SMOOTH),
					0, 0, null);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return img;
		
	}

	@Override
	public BufferedImage scaleImage(final String uri) {
		URL url = null;
		BufferedImage original = null;
		int tbHeight,tbWidth;
		BufferedImage img = null;
		
		try {
			url = new URL(uri);
		} catch (final MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			original = ImageIO.read(url);
		
		tbHeight = (int) (original.getHeight()*tbRatio);
		tbWidth = (int)(original.getWidth()*tbRatio);
		
		img = new BufferedImage(tbWidth, tbHeight, BufferedImage.TYPE_INT_RGB);
		img.createGraphics().drawImage(original.getScaledInstance(tbWidth, tbHeight, Image.SCALE_SMOOTH),
					0, 0, null);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return img;
		
	}

	@Override
	public boolean removeImage(final String uri) {
		final File f = new File(uri);

	    // Make sure the file or directory exists and isn't write protected
	    if (!f.exists()) {
			throw new IllegalArgumentException("Delete: no such file or directory: " + uri);
		}

	    if (!f.canWrite()) {
			throw new IllegalArgumentException("Delete: write protected: " + uri);
		}

	    // If it is a directory, make sure it is empty
	    if (f.isDirectory()) {
	      final String[] files = f.list();
	      if (files.length > 0) {
			throw new IllegalArgumentException("Delete: directory not empty: " + uri);
		}
	    }

	    // Attempt to delete it
	    final boolean success = f.delete();

	    if (!success) {
			throw new IllegalArgumentException("Delete: deletion failed");
		}
	    
	    return success;
	  }

	@Override
	public boolean saveImage(final Image img, final String fileName) {
		final File file = new File(tempFolder + fileName);
		try {
			ImageIO.write(bufferImage(img), "jpg", file);
			return true;
		} catch (final IOException e) {
			e.printStackTrace();
			return false;
		
		}
	}

	
}
